<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Department extends Backend
{

    /**
     * Department模型对象
     * @var \app\admin\model\Department
     */
    protected $model = null;
    protected $noNeedRight = ['indexsearch'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Department;
        $this->relationtTable = 'college';

        $groupIds = $this->auth->getGroupIds();
        //学院单位
        if($groupIds[0]==7)
        {
            $info = (new \app\admin\model\College) -> where('name',$this->auth->username) -> find();
            if(isset($info))
            {
                $departmentId = (new \app\admin\model\Department) -> where('college_id',$info['id']) -> column('id');
                $this -> whereExtend['id'] = ['in',$departmentId];
            }
        }

    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function indexsearch()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this -> selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $whereCollege = [];
            if(input('college_id')){
                $whereCollege = [
                    'college_id' => ['=',input('college_id')]
                ];
            }

            $list = $this->model
                ->where($whereCollege)
                // ->where($where)
                ->order($sort, $order)
                ->select();
            // echo $this -> model -> getLastSql();

            $list = collection($list)->toArray();
            $data = [];
            foreach ($list as $k => $v){
                $data [] = [
                    'name'  => $v['name'],
                    'value' => $v['id']
                ];
            }

            $this -> success('','',$data);
        }
//        return $this->view->fetch();
    }

    public function getbycollegeid($ids=null){
        if(!$ids){
            return null;
        }

        if($this -> request -> isPost()){
            $departments = \app\admin\model\Department::all(['college_id'=>$ids]);


            $this -> success('获取成功',null,$departments);
        }

        $this -> error('非法请求');

    }
}
